package com.ijianji.module_zhuanzhu.ui

import android.view.View
import android.view.WindowManager
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.fenghuajueli.libbasecoreui.dialog.CommonTipsDialog
import com.fenghuajueli.libbasecoreui.viewmodel.BaseViewModelActivity2
import com.fenghuajueli.module_route.ZhuanZhuModuleRoute
import com.ijianji.module_zhuanzhu.databinding.ActivityZhuanZhuStartBinding
import com.ijianji.module_zhuanzhu.model.ZhuanZhuModel
import com.kongzue.dialog.interfaces.OnDialogButtonClickListener
import com.kongzue.dialog.util.DialogSettings
import com.kongzue.dialog.v3.MessageDialog

@Route(path = ZhuanZhuModuleRoute.ACTIVITY_ZHUANZHU_START)
class ZhuanZhuStartActivity : BaseViewModelActivity2<ZhuanZhuModel, ActivityZhuanZhuStartBinding>() {

    @Autowired(name = "duration")
    @JvmField
    var duration: Int = 0

    @Autowired(name = "model")
    @JvmField
    var absorbedModel: Int = 0  //默认是普通专注模式

    @Autowired(name = "timemodel")
    @JvmField
    var timeModel: Int = 0  //默认25分钟

    override fun createViewModel(): ZhuanZhuModel {
        return ZhuanZhuModel()
    }

    override fun createViewBinding(): ActivityZhuanZhuStartBinding {
        return ActivityZhuanZhuStartBinding.inflate(layoutInflater)
    }

    override fun initView() {
        DialogSettings.isUseBlur = false       //是否开启模糊效果，默认关闭
        DialogSettings.style = (DialogSettings.STYLE.STYLE_IOS)        //全局主题风格，提供三种可选风格，STYLE_MATERIAL, STYLE_KONGZUE, STYLE_IOS
        DialogSettings.theme = (DialogSettings.THEME.LIGHT)
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
        when (absorbedModel) {
            0 -> {
                binding.tvCategoryName.text = "普通专注"
                binding.tvCategoryDesc.text = "适用于简单日常任务"
            }
            1 -> {
                binding.tvCategoryName.text = "学霸模式"
                binding.tvCategoryDesc.text = "高度集中注意力，抵制手机诱惑"
            }
            2 -> {
                binding.tvCategoryName.text = "考试模式"
                binding.tvCategoryDesc.text = "锻炼考试用时分配，掌握解题节奏"
            }
        }
        setOnClick()
        if (timeModel == 3) {
            binding.tvShowTime.text = "00:00"
        } else {
            binding.tvShowTime.text = "${duration}:00"
        }
        binding.myActionBar.setLeftIconClick {
            onBackPressed()
        }
        model.timeShowData.observe(this, Observer {
            binding.tvShowTime.text = it
            if ("00:00" == it && timeModel != 3) {
                binding.btnEnd.visibility = View.GONE
                binding.btnResume.visibility = View.GONE
                binding.btnPause.visibility = View.GONE
                showEndTipsDialog()
            }
        })
        model.progressData.observe(this, Observer {
            binding.circleProgress.progress = it
        })
        model.startDownTime(timeModel, duration * 60, true)
    }


    private fun setOnClick() {
        binding.btnEnd.setOnClickListener {
            showTipsDialog()
        }
        binding.btnPause.setOnClickListener {
            model.stopTime()
            binding.btnPause.visibility = View.GONE
            binding.btnEnd.visibility = View.VISIBLE
            binding.btnResume.visibility = View.VISIBLE
        }
        binding.btnResume.setOnClickListener {
            model.resumeTime()
            binding.btnEnd.visibility = View.GONE
            binding.btnResume.visibility = View.GONE
            binding.btnPause.visibility = View.VISIBLE
        }
    }


    private fun showEndTipsDialog() {
        MessageDialog.show(this, "提示", "恭喜你完成了专注！", "确定")
                .onOkButtonClickListener = OnDialogButtonClickListener { baseDialog, v ->
            finish()
            true
        }
    }

    private fun showTipsDialog() {
        val dialog = CommonTipsDialog(this, "当前正在专注中，确定退出吗？",
                "取消", "退出")
        dialog.setConfirmListener {
            finish()
        }
        dialog.setCancelListener {

        }
        dialog.setCancelable(false)
        dialog.show()

    }

    override fun onDestroy() {
        super.onDestroy()
        model.disposable?.dispose()
    }

    override fun onBackPressed() {
        if (model.isEndTime) {
            super.onBackPressed()
        } else {
            showTipsDialog()
        }
    }

}